package org.example.shoppingmall.service.impl;

import org.example.shoppingmall.dao.CartDao;
import org.example.shoppingmall.dao.OpenDao;
import org.example.shoppingmall.entity.Cart;
import org.example.shoppingmall.entity.Goods;
import org.example.shoppingmall.service.CartService;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

public class CartServiceImpl implements CartService {
    CartDao cartDao = new CartDao();
    /**
     * 删除购物车里已选商品
     * @param goods_id
     * @return
     */
    @Override
    public boolean goodsDelete(Integer goods_id,Integer user_id) {
        return cartDao.goodsDelete(goods_id,user_id);
    }

    /**
     * 修改购物车内某商品购买数量
     * @param goods_id
     * @param quantity
     * @return
     */
    @Override
    public boolean cartUpdate(Integer goods_id, Integer quantity,Integer user_id) {
        return cartDao.cartUpdate(goods_id,quantity,user_id);
    }
    /**
     * 获取购物车内商品列表并返回
     * @param page_size
     * @param page
     * @return
     */
    @Override
    public List<Goods> goodsSearchInCart(Integer page_size, Integer page,Integer user_id) {
        return cartDao.goodsSearchInCart(page_size,page,user_id);
    }
    /**
     * 记录购物车内有多少条商品数据
     * @param
     * @param
     * @return
     */
    public Integer goodsCountInCart(Integer user_id){
        return cartDao.goodsCountInCart(user_id);
    }

    /**
     * 选取购物车内要买的商品
     * @param goodsIDList
     * @return
     */
    @Override
    public List<Cart> getInfoFromCart(List<Integer> goodsIDList) {
        List<Cart> cartList = new ArrayList<>(goodsIDList.size());
        for(Integer goodsId:goodsIDList){
            cartList.add(cartDao.getInfoFromCart(goodsId));
        }
        return cartList;
    }

    /**
     * 将购物车结算成订单(二重判断之后)
     * @param address
     * @param phone
     * @return
     */
    @Override
    public boolean settleIntoOrder(String address, String phone, List<Cart> cartList) {
        Integer rows = 0;
        for(Cart cart:cartList){
            rows += cartDao.settleIntoOrder(address,phone,cart);
        }
        if(rows==cartList.size()){
            return true;
        }
        return false;
    }

    /**
     * 结算账户余额(二重判断之后)
     * @param id
     * @param amount
     * @return
     */
    @Override
    public boolean reduceBalance(Integer id,BigDecimal amount){
        return cartDao.reduceBalance(id,amount);
    }

    /**
     * 修改商品库存(二重判断之后)
     * @param goodsIDList
     * @return
     */
    @Override
    public boolean updateStock(List<Integer> goodsIDList,List<Integer> stock) {
        Integer rows = 0;
        for(Integer goodsId:goodsIDList){
            rows += cartDao.updateStock(goodsId,stock.indexOf(goodsId));
        }
        if(rows==goodsIDList.size()){
            return true;
        }else {
            return false;
        }
    }

    /**
     * 通过比较要花费的钱和余额判断是否能结账（判断一）
     * @param goodsIdList
     * @return
     */
    @Override
    public boolean moneyJudging(List<Integer> goodsIdList,Integer user_id){
        BigDecimal balance = cartDao.getBalance(user_id);
        BigDecimal amount = BigDecimal.valueOf(0);
        for(Integer cartId:goodsIdList){
            amount = amount.add(cartDao.accountsReceviable(cartId,user_id));
            if(amount.compareTo(balance) > 0){
                return false;
            }
        }
        return true;
    }

    /**
     * 通过比较要购买数量和库存判断是否能结账（判断二）
     * @param goodsIdList
     * @return
     */
    @Override
    public boolean quantityJudging(List<Integer> goodsIdList,Integer user_id) {
        for (Integer goodsId:goodsIdList) {
            Integer quantity = cartDao.getQuantity(goodsId,user_id);
            Integer stock = cartDao.getStock(goodsId);
            if(quantity>stock){
                return false;
            }
        }
        return true;
    }

    /**
     * 结算后库存的数量
     * @param goodsIdList
     * @return
     */
    @Override
    public List<Integer> stockCalculate(List<Integer> goodsIdList,Integer user_id) {
        List<Integer> stockResults = new ArrayList<>(goodsIdList.size());
        for (Integer goodsId : goodsIdList) {
            Integer stock = cartDao.getStock(goodsId);
            Integer quantity = cartDao.getQuantity(goodsId,user_id);
            Integer stockAfterSale = stock - quantity;
            stockResults.add(stockAfterSale);
//            System.out.println("stockAfterSale:"+stockAfterSale);
        }
        return stockResults;
    }

    /**
     * 结算后余额的多少
     * @param goodsIdList
     * @param user_id
     * @return
     */
    @Override
    public BigDecimal balanceCalculate(List<Integer> goodsIdList,Integer user_id) {
        BigDecimal balance = cartDao.getBalance(user_id);
        BigDecimal amount = BigDecimal.valueOf(0);
        for(Integer goodsId:goodsIdList){
            amount = amount.add(cartDao.accountsReceviable(goodsId,user_id));
        }
        balance = balance.subtract(amount);
//        System.out.println("balance:" + balance);
        return balance;
    }

}
